import '../generate.dart';
import 'package:flutter_code_push_next/index.dart';

class Register {
  /// File path, (class name, WTVMBaseType)
  late Map<String, Map<String, WTVMBaseType>> _registerMap;

  Future<void> init() async {
    List<WTVMBaseType> typeList = [
      WTThemeData(),
      WTPageStorageKey(),
      WTScrollNotification(),
      WTAlign(),
      WTTextEditingValue(),
      WTPageTransitionsTheme(),
      WTUniqueKey(),
      WTTopVariable10(),
      WTSnackBar(),
      WTSliderComponentShape(),
      WTBottomNavigationBarItem(),
      WTIndexedStack(),
      WTScrollMetrics(),
      WTRect(),
      WTGalleryLocalizations(),
      WTAnimatedSize(),
      WTAlwaysStoppedAnimation(),
      WTSimulation(),
      WTTextInputAction(),
      WTAppBarTheme(),
      WTShapeBorder(),
      WTSpacer(),
      WTDirectionality(),
      WTBorderRadiusDirectional(),
      WTSliverChildBuilderDelegate(),
      WTChangeNotifierProvider(),
      WTOrdinalSortKey(),
      WTDragStartBehavior(),
      WTAnimationController(),
      WTSnackBarBehavior(),
      WTSharedAxisPageTransitionsBuilder(),
      WTIconButton(),
      WTGoogleFonts(),
      WTLinearProgressIndicator(),
      WTFormFieldState(),
      WTLocaleNames(),
      WTOverlayState(),
      WTAppBar(),
      WTFadeScaleTransition(),
      WTClipboardData(),
      WTBoxShape(),
      WTTextEditingController(),
      WTRestorableDateTime(),
      WTClipOval(),
      WTStadiumBorder(),
      WTSafeArea(),
      WTChip(),
      WTRestorableListenable(),
      WTTopVariable2(),
      WTTickerFuture(),
      WTAnimatedSwitcher(),
      WTMaterialApp(),
      WTSnackBarAction(),
      WTNeverScrollableScrollPhysics(),
      WTAssetImage(),
      WTPageView(),
      WTFunction(),
      WTPackageInfo(),
      WTValueListenableBuilder(),
      WTClipRRect(),
      WTDividerThemeData(),
      WTMainAxisSize(),
      WTTimer(),
      WTAlignment(),
      WTOpenContainer(),
      WTPaginatedDataTable(),
      WTEdgeInsetsGeometryTween(),
      WTInheritedWidget(),
      WTRestorableInt(),
      WTCupertinoTextField(),
      WTKey(),
      WTInteractiveViewer(),
      WTNotificationListener(),
      WTTextOverflow(),
      WTImageIcon(),
      WTRestorationScope(),
      WTSingleChildScrollView(),
      WTBoxFit(),
      WTTimePickerDialog(),
      WTLocalizations(),
      WTOverlayEntry(),
      WTNumberFormat(),
      WTCupertinoSwitch(),
      WTObjectKey(),
      WTChipThemeData(),
      WTBorderDirectional(),
      WTIconData(),
      WTRenderBox(),
      WTTimeOfDay(),
      WTDateTime(),
      WTEdgeInsetsGeometry(),
      WTInputDecorationTheme(),
      WTTopVariable3(),
      WTTolerance(),
      WTWrapAlignment(),
      WTRoundedRectangleBorder(),
      WTSwitch(),
      WTCurvedAnimation(),
      WTRestorableTextEditingController(),
      WTDatePickerDialog(),
      WTPageRouteBuilder(),
      WTScrollDirection(),
      WTAnimation(),
      WTSnackBarThemeData(),
      WTThemeMode(),
      WTDefaultTabController(),
      WTSliverPadding(),
      WTListener(),
      WTShapeBorderClipper(),
      WTRestorationBucket(),
      WTSliverList(),
      WTRotationTransition(),
      WTCanvas(),
      WTExcludeSemantics(),
      WTBrightness(),
      WTCupertinoTabView(),
      WTTab(),
      WTWidget(),
      WTInkWell(),
      WTSet(),
      WTNavigationRailDestination(),
      WTCurves(),
      WTCupertinoDatePicker(),
      WTTopVariable11(),
      WTDrawer(),
      WTPreferredSize(),
      WTInkResponse(),
      WTAnimatedContainer(),
      WTWillPopScope(),
      WTAnimatedList(),
      WTMaterialColor(),
      WTdouble(),
      WTFractionalTranslation(),
      WTTopVariable4(),
      WTSelector(),
      WTSingleTickerProviderStateMixin(),
      WTCupertinoSegmentedControl(),
      WTInputChip(),
      WTTheme(),
      WTState(),
      WTScrollbar(),
      WTCustomScrollView(),
      WTAnimatedPadding(),
      WTAnimationBehavior(),
      WTTextSelection(),
      WTRestorableIntN(),
      WTTopVariable20(),
      WTExpanded(),
      WTLocale(),
      WTColumn(),
      WTWidgetsBinding(),
      WTEdgeInsetsDirectional(),
      WTComparable(),
      WTTextFormField(),
      WTAlignmentDirectional(),
      WTThumb(),
      WTPath(),
      WTPositionedTransition(),
      WTImageConfiguration(),
      WTNavigationRail(),
      WTFilterChip(),
      WTBorderStyle(),
      WTCubic(),
      WTPositionedDirectional(),
      WTTopVariable16(),
      WTTopVariable8(),
      WTRandom(),
      WTIcons(),
      WTAnnotatedRegion(),
      WTnum(),
      WTMediaQueryData(),
      WTCupertinoDialogAction(),
      WTSystemUiOverlayStyle(),
      WTAspectRatio(),
      WTCupertinoTimerPicker(),
      WTStatelessWidget(),
      WTLocalizationsDelegate(),
      WTBeveledRectangleBorder(),
      WTSchedulerBinding(),
      WTBottomAppBar(),
      WTCrossAxisAlignment(),
      WTFocusTraversalGroup(),
      WTTickerProviderStateMixin(),
      WTIconThemeData(),
      WTScaffoldMessenger(),
      WTPaint(),
      WTCurve(),
      WTContainerTransitionType(),
      WTBorderRadiusGeometry(),
      WTMainAxisAlignment(),
      WTShowValueIndicator(),
      WTIterator(),
      WTCupertinoSlider(),
      WTColorTween(),
      WTScrollSpringSimulation(),
      WTPlatform(),
      WTFloatingLabelBehavior(),
      WTBorderRadius(),
      WTTabBar(),
      WTSimpleDialog(),
      WTListTile(),
      WTMaterialType(),
      WTHitTestBehavior(),
      WTCupertinoSlidingSegmentedControl(),
      WTFontWeight(),
      WTCheckedPopupMenuItem(),
      WTStackFit(),
      WTBuilder(),
      WTStack(),
      WTAutovalidateMode(),
      WTAnimationStatus(),
      WTGlobalKey(),
      WTFlexible(),
      WTSlider(),
      WTBottomNavigationBar(),
      WTNumericFocusOrder(),
      WTPageController(),
      WTFittedBox(),
      WTSliderTheme(),
      WTCupertinoTabBar(),
      WTCupertinoContextMenuAction(),
      WTColor(),
      WTMaterialPageRoute(),
      WTModel(),
      WTTopVariable17(),
      WTTopVariable9(),
      WTRestorableBool(),
      WTBackButton(),
      WTRawMaterialButton(),
      WTFlexFit(),
      WTMaterialAccentColor(),
      WTGridView(),
      WTClampingScrollPhysics(),
      WTRegExp(),
      WTStaggeredTile(),
      WTMatrix4Tween(),
      WTVisibility(),
      WTBottomAppBarTheme(),
      WTOverflowBox(),
      WTTopVariable21(),
      WTRangeValues(),
      WTConsumer(),
      WTRestorableTimeOfDay(),
      WTDataCell(),
      WTTextInputType(),
      WTDataTableSource(),
      WTFocus(),
      WTSizeTransition(),
      WTbool(),
      WTScaleStartDetails(),
      WTDefaultTextStyle(),
      WTTopVariable5(),
      WTOrderedTraversalPolicy(),
      WTFocusTraversalOrder(),
      WTClip(),
      WTPhysicalShape(),
      WTIconTheme(),
      WTStaggeredGridView(),
      WTImage(),
      WTScaleTransition(),
      WTValueNotifier(),
      WTPositioned(),
      WTDataColumn(),
      WTWidgetOrderTraversalPolicy(),
      WTCircleAvatar(),
      WTFadeTransition(),
      WTDecorationImage(),
      WTUserScrollNotification(),
      WTCustomPainter(),
      WTChangeNotifier(),
      WTGestureDetector(),
      WTFadeThroughTransition(),
      WTTopVariable22(),
      WTCupertinoSliverNavigationBar(),
      WTCheckbox(),
      WTNotchedShape(),
      WTUserAccountsDrawerHeader(),
      WTStatefulWidget(),
      WTOffset(),
      WTint(),
      WTAdaptiveWindowType(),
      WTTextCapitalization(),
      WTTopVariable6(),
      WTTopVariable18(),
      WTSwitchListTile(),
      WTRow(),
      WTBoxConstraints(),
      WTPopupMenuDivider(),
      WTTabBarView(),
      WTFocusScope(),
      WTPreferredSizeWidget(),
      WTNavigationRailLabelType(),
      WTSharedAxisTransition(),
      WTRichText(),
      WTPopupMenuEntry(),
      WTOverlay(),
      WTLayoutBuilder(),
      WTIntl(),
      WTCupertinoNavigationBar(),
      WTFlutterLogo(),
      WTRestorableDouble(),
      WTTextInputFormatter(),
      WTColorScheme(),
      WTPaintingContext(),
      WTRestorableStringN(),
      WTTextSelectionThemeData(),
      WTTextStyle(),
      WTGlobalMaterialLocalizations(),
      WTFloatingActionButton(),
      WTTapUpDetails(),
      WTTextAlign(),
      WTRangeLabels(),
      WTWrapCrossAlignment(),
      WTTopVariable14(),
      WTMaterialBanner(),
      WTTweenSequence(),
      WTCupertinoDialogRoute(),
      WTCupertinoDatePickerMode(),
      WTDateFormat(),
      WTRestorationMixin(),
      WTTransform(),
      WTStatefulBuilder(),
      WTFocusNode(),
      WTTopVariable15(),
      WTString(),
      WTCupertinoActionSheetAction(),
      WTLinearGradient(),
      WTDateTimeRange(),
      WTTextButton(),
      WTRepaintBoundary(),
      WTFutureBuilder(),
      WTText(),
      WTScopedModelDescendant(),
      WTConstrainedBox(),
      WTRadius(),
      WTAnimatedListState(),
      WTForm(),
      WTLogicalKeyboardKey(),
      WTRadioListTile(),
      WTCupertinoPageScaffold(),
      WTAnimatedWidget(),
      WTBorderRadiusTween(),
      WTKeyEventResult(),
      WTDecoratedBox(),
      WTBuildContext(),
      WTCupertinoAlertDialog(),
      WTObject(),
      WTTabController(),
      WTRawKeyDownEvent(),
      WTTweenSequenceItem(),
      WTMediaQuery(),
      WTOpacity(),
      WTCupertinoButton(),
      WTMatrix4(),
      WTScrollPhysics(),
      WTRelativeRectTween(),
      WTCustomPainterSemantics(),
      WTTopVariable7(),
      WTTypography(),
      WTTopVariable19(),
      WTVertexMode(),
      WTAnimatable(),
      WTChoiceChip(),
      WTFontStyle(),
      WTScrollController(),
      WTVisualDensity(),
      WTDragEndDetails(),
      WTTween(),
      WTTopVariable23(),
      WTPaintingStyle(),
      WTRelativeRect(),
      WTCupertinoContextMenu(),
      WTSize(),
      WTCupertinoTabScaffold(),
      WTNotification(),
      WTReadingOrderTraversalPolicy(),
      WTCircularNotchedRectangle(),
      WTVertices(),
      WTExactAssetImage(),
      WTStringBuffer(),
      WTRoute(),
      WTSimpleDialogOption(),
      WTInputBorder(),
      WTMaxLengthEnforcement(),
      WTIcon(),
      WTCupertinoColors(),
      WTRouteSettings(),
      WTInterval(),
      WTClipRect(),
      WTMaterialScrollBehavior(),
      WTIgnorePointer(),
      WTInk(),
      WTNavigatorState(),
      WTDivider(),
      WTLocaleNamesLocalizationsDelegate(),
      WTScaffoldMessengerState(),
      WTContainer(),
      WTToggleButtons(),
      WTPopupMenuItem(),
      WTColors(),
      WTTextField(),
      WTTextSpan(),
      WTProvider(),
      WTTopVariable12(),
      WTCupertinoPageRoute(),
      WTFilteringTextInputFormatter(),
      WTMap(),
      WTRestorableRouteFuture(),
      WTAlertDialog(),
      WTCupertinoModalPopupRoute(),
      WTDismissDirection(),
      WTRadio(),
      WTSizedBox(),
      WTBottomNavigationBarType(),
      WTCurveTween(),
      WTTransformationController(),
      WTScaffold(),
      WTCard(),
      WTOutlineInputBorder(),
      WTCupertinoThemeData(),
      WTBlendMode(),
      WTCustomPaint(),
      WTSemanticsService(),
      WTIterable(),
      WTOverflowBar(),
      WTPageTransitionSwitcher(),
      WTDuration(),
      WTPoint(),
      WTCupertinoActionSheet(),
      WTCenter(),
      WTValueKey(),
      WTPadding(),
      WTWrap(),
      WTLinkedHashMap(),
      WTMaterial(),
      WTFuture(),
      WTElevatedButton(),
      WTVerticalDivider(),
      WTPopupMenuButton(),
      WTRawKeyboardListener(),
      WTTextTheme(),
      WTModalBarrier(),
      WTExcludeFocus(),
      WTClipboard(),
      WTDecoration(),
      WTNavigator(),
      WTCircularProgressIndicator(),
      WTTargetPlatform(),
      WTBackButtonIcon(),
      WTRangeSlider(),
      WTSliverGridDelegateWithFixedCrossAxisCount(),
      WTAxis(),
      WTRangeSliderThumbShape(),
      WTIntrinsicHeight(),
      WTDialogRoute(),
      WTDataRow(),
      WTRestorableProperty(),
      WTReverseAnimation(),
      WTDismissible(),
      WTOverlayVisibilityMode(),
      WTTopVariable1(),
      WTCircleBorder(),
      WTSlideTransition(),
      WTMultiProvider(),
      WTLicensePage(),
      WTGridTile(),
      WTBouncingScrollPhysics(),
      WTGlobalWidgetsLocalizations(),
      WTOutlinedButton(),
      WTAlwaysScrollableScrollPhysics(),
      WTGridTileBar(),
      WTInputDecoration(),
      WTFormState(),
      WTIterableMixin(),
      WTRRect(),
      WTCupertinoActivityIndicator(),
      WTDragUpdateDetails(),
      WTTextDirection(),
      WTRotatedBox(),
      WTScaffoldState(),
      WTScopedModel(),
      WTTooltip(),
      WTCupertinoTheme(),
      WTNavigationRailThemeData(),
      WTTapGestureRecognizer(),
      WTSharedAxisTransitionType(),
      WTFloatingActionButtonLocation(),
      WTBottomSheetThemeData(),
      WTTopVariable13(),
      WTGlobalCupertinoLocalizations(),
      WTListView(),
      WTBoxDecoration(),
      WTSemanticsProperties(),
      WTVector3(),
      WTEdgeInsets(),
      WTBorderSide(),
      WTAnimatedBuilder(),
      WTImageProvider(),
      WTRestorableBoolN(),
      WTSliderThemeData(),
      WTSemantics(),
      WTDateRangePickerDialog(),
      WTList(),
      WTMergeSemantics(),
      WTCupertinoSliverRefreshControl(),
      WTBoxPainter(),
      WTBorder(),
      WTActionChip(),
      WTTextPainter(),
      WTCupertinoIcons(),
      WTMaterialLocalizations(),
    ];

    _registerMap = Map();
    for (var item in typeList) {
      var path = item.definePath;
      var containsKey = _registerMap.containsKey(path);
      if (containsKey == false) {
        _registerMap[path] = Map<String, WTVMBaseType>();
      }
      Map<String, WTVMBaseType> registerMap = _registerMap[path]!;
      registerMap[item.defineName] = item;
    }
  }

  void registerEnv(Environment env, Define define) {
    var prefix = define.prefix;
    if (prefix != null) {
      Environment prefixEnv;
      if (env.containsKey(prefix) == false) {
        prefixEnv = Environment();
        env.set(prefix, prefixEnv, isDirect: true);
      } else {
        prefixEnv = env.get(prefix) as Environment;
      }
      env = prefixEnv;
    }

    var name = define.name;
    var definePath = define.path;
    Map<String, WTVMBaseType>? m = _registerMap[definePath];
    if (m == null) throw "Path not found. $definePath";
    if (m.containsKey(name)) {
      WTVMBaseType b = m[name]!;
      env.set(name, b, isDirect: true);
      return;
    } else {
      WTVMBaseType b = m['dynamic']!;

      bool _internal(Map<String, dynamic>? n, bool isGetOrSet) {
        if (n?.containsKey(name) == true) {
          if (isGetOrSet == false) {
            var value = n![name]!;
            env.set(name, value, isDirect: true);
          } else {
            /// Value cannot be set here, BaseType controls GetValue and SetValue
            env.set(name, b, isDirect: true);
          }
          return true;
        }
        return false;
      }

      var attributesMap = b.attributesMap;
      var getAttributeMap = b.getAttributeMap;
      var setAttributeMap = b.setAttributeMap;
      var value = _internal(attributesMap, false) ||
          _internal(getAttributeMap, true) ||
          _internal(setAttributeMap, true);
      if (value) return;
    }
    throw "Could not find $name definition";
  }
}
